# Contributor: Robert White <rjwhite2453@gmail.com>
# Contributor: Kevin Daudt <kdaudt@alpinelinux.org>
# Maintainer: Kevin Daudt <kdaudt@alpinelinux.org>
pkgname=fzf
pkgver=0.22.0
pkgrel=2
pkgdesc="A command-line fuzzy finder"
url="https://github.com/junegunn/fzf"
arch="all !mips !mips64"
license="MIT"
makedepends="go bash tmux"
options="net"
subpackages="
	$pkgname-tmux::noarch
	$pkgname-bash-completion:bashcomp:noarch
	$pkgname-zsh-completion:zshcomp:noarch
	$pkgname-vim:vim:noarch
	$pkgname-neovim:nvim:noarch
	$pkgname-doc
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/junegunn/fzf/archive/$pkgver.tar.gz
	find-buzybox-compat.patch
	"

export GOPATH="$srcdir"

build() {
	go build
}

check() {
	# shellcheck disable=SC1007
	SHELL=/bin/sh GOOS= go test -v \
		github.com/junegunn/fzf/src \
		github.com/junegunn/fzf/src/algo \
		github.com/junegunn/fzf/src/tui \
		github.com/junegunn/fzf/src/util
}

package() {
	install -Dm0755 fzf "$pkgdir"/usr/bin/fzf

	install -D man/man1/fzf.1 "$pkgdir"/usr/share/man/man1/fzf.1
	install -D man/man1/fzf-tmux.1 "$pkgdir"/usr/share/man/man1/fzf-tmux.1
}

tmux() {
	depends="tmux bash"
	pkgdesc="Helper script to start fzf in a tmux pane"

	cd "$builddir"
	install -Dm0755 bin/fzf-tmux "$subpkgdir"/usr/bin/fzf-tmux
}

bashcomp() {
	pkgdesc="additional scripts for bash like shell completion and keybindings"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	cd "$builddir"
	install -Dm0644 shell/completion.bash \
		"$subpkgdir"/usr/share/bash-completion/completions/$pkgname
	mkdir -p "$subpkgdir"/usr/share/fzf/
	install -m0644 shell/key-bindings.bash "$subpkgdir"/usr/share/fzf/
}

zshcomp() {
	pkgdesc="additional scripts for zsh like shell completion and keybindings"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel zsh"

	cd "$builddir"
	install -Dm0644 shell/completion.zsh \
		"$subpkgdir"/usr/share/zsh/site-functions/_$pkgname
	mkdir -p "$subpkgdir"/usr/share/fzf/
	install -m0644 shell/key-bindings.zsh "$subpkgdir"/usr/share/fzf/
}

vim() {
	pkgdesc="fzf vim plugin"
	install="$pkgname-vim.post-install"
	install_if="$pkgname=$pkgver-r$pkgrel vim"
	cd "$builddir"
	install -Dm0644 plugin/fzf.vim "$subpkgdir"/usr/share/vim/vimfiles/plugin/fzf.vim
	install -Dm0644 doc/fzf.txt "$subpkgdir"/usr/share/vim/vimfiles/doc/fzf.txt
}

nvim() {
	pkgdesc="fzf vim plugin for neovim"
	install_if="$pkgname=$pkgver-r$pkgrel neovim"
	cd "$builddir"
	install -Dm0644 plugin/fzf.vim "$subpkgdir"/usr/share/nvim/runtime/plugin/fzf.vim
	install -Dm0644 doc/fzf.txt "$subpkgdir"/usr/share/nvim/runtime/doc/fzf.txt
}

cleanup_srcdir() {
	export GOPATH="$srcdir"
	go clean -modcache
	default_cleanup_srcdir
}

sha512sums="003b187c93f0ab2e8a1adbd4eb9586e05e12fb9a86e445ac8ff48ba391ae16f104d60d18934bb46765537d5007035f549c15d6970c901f72813d55001d3bc6e5  fzf-0.22.0.tar.gz
c59e7eb712f5072a2c66b79800f7674d1f9208b1ead4f654e57f8dcb673a45a751725a005c89759847f78c7ae7f246980b2c94e5fff5a3e8c2f6a364965e567e  find-buzybox-compat.patch"
